// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DBContainerResourceHelpers.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.DataModel
{
    #region

    using System;
    using System.Linq;

    #endregion

    /// <summary>
    /// The db container resource helpers.
    /// </summary>
    public class DBContainerResourceHelpers
    {
        #region Public Methods

        /// <summary>
        /// The add.
        /// </summary>
        /// <param name="containerId">
        /// The containerId.
        /// </param>
        /// <param name="resource">
        /// The resource.
        /// </param>
        /// <returns>
        /// The add.
        /// </returns>
        public static bool Add(Guid containerId, DBResource resource)
        {
            // Look if the resource is already there 
            var query = from r in Singleton<DBFramework>.Instance.Entities.DBContainerResource
                        where r.DBResource.ID == resource.ID
                        where r.DBContainer.ID == containerId
                        select r;

            // Already there !
            if (query.Count() > 0)
            {
                return false;
            }

            // If not add the resource
            var dbContainerResource = DBContainerResource.CreateDBContainerResource(Guid.NewGuid());

            dbContainerResource.DBContainer = DBContainerHelpers.GetContainer(containerId);
            dbContainerResource.DBResource = resource;
            Singleton<DBFramework>.Instance.Entities.AddToDBContainerResource(dbContainerResource);

            var n = 0;
            try
            {
                n = Singleton<DBFramework>.Instance.Commit();
            }
            catch
            {
            }

            // Done
            return n > 0;
        }

        #endregion
    }
}